package com.example.usercenter.exception;

import com.example.usercenter.common.Result;
import com.example.usercenter.enums.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author hhf
 * @Date 2024/8/29
 * @Description 利用 spring AOP 实现
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {



    @ExceptionHandler(value = {BusinessException.class})
    public Result businessExceptionHandler(BusinessException e){

        log.error("BusinessException:" + e.getMessage(),e);

        return Result.fail(e.getCode(),e.getMessage(), e.getDescription());
    }

    @ExceptionHandler(value = {RuntimeException.class})
    public Result runtimeException(RuntimeException e){

        log.error("RuntimeException:",e);

        return Result.fail(StatusEnum.SYSTEM_ERROR,e.getMessage());
    }
}
